<?php
/**
 * 一元包邮
 * ============================================================================
 * $Author: 王海龙 $
 * $Id: buyone.php 2015-01-01 王海龙 $
*/

/** 注意缓存数据的初始化
 * buyoneStock_{$id}      计数器：商品的库存 $id 为一元包邮parent_id
 * buyoneQueue_{$id}      列队：商品购买成功的队列 $id 为一元包邮parent_id
 * buyoneQueue2_{$id}     列队：已发放商品购买成功的队列 $id 为一元包邮parent_id
 * buyoneBaoyouQueue      列队：参与抢购失败用户，需要发放包邮卷的列队
 * buyoneBaoyouQueue2     列队：已发放包邮卷的列队
 * sumBuyoneUserNum       计数：总参与客户量.
 * sumBuyoneUserNum2      计数：真实参与用户量
 * setSumBuyone           计数器：分流总量
 */


require(dirname(__FILE__) . '/includes/init.php');
@ini_set('display_errors',        1);
include_once(ROOT_PATH . 'includes/funBuyone.php');

    $smarty->assign('start_time', strtotime('2015-09-18 22:00:00'));
    $smarty->assign('end_time', strtotime('2015-09-19 22:00:00'));
    $smarty->assign('start_date', '09月18日22:00');
    $smarty->assign('end_date', '09月19日22:00');

/* AJAX抢购操作 */
if (!empty($_GET['act']) && $_GET['act'] == 'get') {
    // 如果登录进行查询
    if (!empty($_SESSION['user_id']) && !empty($_REQUEST['id'])) {
        include_once(ROOT_PATH . "includes/classQueue.php");
        $q = new classQueue(); // 列队
        include_once(ROOT_PATH . 'includes/classCounter.php');
        $c = new classCounter(); // 计数器

        $id = intval($_REQUEST['id']);
        /* 加载计数器处理类库 */
        if (empty(M()->get("buyoneIsBuy_{$_SESSION['user_id']}")) && $c->deCounter("buyoneStock_{$id}")) {
            // 减库存成功进行队列处理
            // 添加至队列中
            if ($q->append("buyoneQueue", "{$_SESSION['user_id']}:{$id}")) {
                // 记录当前用户已经抢购成功
                M()->set("buyoneIsBuy_{$_SESSION['user_id']}", '1601', 86400);
                // 返回成功
                $result['error'] = '1601';
                $result['content'] = $smarty->fetch("buyone/success.lbi");
            } else {
                // 记录当前用户已经抢购失败
                M()->set("buyoneIsBuy_{$_SESSION['user_id']}", '1603', 86400);
                // 追加包邮队列
                $q->append("buyoneBaoyouQueue", "{$_SESSION['user_id']}");
                // 返回失败
                $result['error'] = '1603';
                $result['content'] = $smarty->fetch("buyone/failure.lbi");
            }
        }
        elseif (M()->get("buyoneIsBuy_{$_SESSION['user_id']}") == '1601') {
            // 重复抢购返回失败
            $result['error'] = '1604';
            $result['content'] = $smarty->fetch("buyone/repeat.lbi");
        }
        elseif (M()->get("buyoneIsBuy_{$_SESSION['user_id']}") > 1601) {
            // 重复抢购返回失败
            $result['error'] = '1604';
            $result['content'] = $smarty->fetch("buyone/repeat2.lbi");
        }
        else {
            // 记录当前用户已经抢购失败
            M()->set("buyoneIsBuy_{$_SESSION['user_id']}", '1602', 86400);
            // 追加包邮队列
            $q->append("buyoneBaoyouQueue", "{$_SESSION['user_id']}");
            // 库存不足返回失败
            $result['error'] = '1602';
            $result['content'] = $smarty->fetch("buyone/failure.lbi");
        }
        // 加载JSON处理类
        include_once(ROOT_PATH . 'includes/cls_json.php');
        $json = new JSON;
        echo $json->encode($result);
        exit;
    }
    exit;
}

// 前端页面------------------------------------------------------------------

/* 往期回顾 */
if (!empty($_GET['act']) && $_GET['act'] == 'wqhg') {
    $smarty->assign('page_title',      '鸡蛋美术商城-一元包邮专区'); // 页面标题
    $smarty->assign('keywords',        '一元、包邮、一元包邮、' . htmlspecialchars($_CFG['shop_keywords'])); // SEO网站关键词
    $smarty->assign('description',     htmlspecialchars($_CFG['shop_desc'])); // SEO网站描述
    $q = !empty($_GET['q']) ? intval($_GET['q']) : 1;
    if (!in_array($q, array(1,2,3,4,5))) {
        exit;
    }
    $wqhg = $smarty->fetch("buyone/wqhg{$q}.html");
    $smarty->assign('wqhg',      $wqhg);
    $smarty->display('buyone/wqhg.dwt');
    exit;
}




// 如果登录进行分流
if (!empty($_SESSION['user_id'])) {
    // 如果监测为不可购买的会员
    isBuyone();
}
/* 显示参与抢购人数 */
if ($sumBuyoneUserNum = M()->get("sumBuyoneUserNum")) {
    $smarty->assign('sumBuyoneUserNum', $sumBuyoneUserNum);
}

$smarty->assign('buyoneList',     getBuyoneList()); // 一元包邮商品列表
$smarty->assign('page_title',      '鸡蛋美术商城-一元包邮专区'); // 页面标题
$smarty->assign('keywords',        '一元、包邮、一元包邮、' . htmlspecialchars($_CFG['shop_keywords'])); // SEO网站关键词
$smarty->assign('description',     htmlspecialchars($_CFG['shop_desc'])); // SEO网站描述
$smarty->display('buyone/buyone.dwt');

